package com.zhijian.wharf.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zhijian.wharf.entity.dto.EnvMonitorDataDto;
import com.zhijian.wharf.entity.po.EnvMonitor;
import com.zhijian.wharf.enums.EnvMonitorEnum;
import com.zhijian.wharf.service.EnvMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境监测数据同步任务
 *
 * @author HCG
 * @version 1.0
 * @date 2024-07-24
 */
@Service
@Slf4j
public class EnvMonitorDataSyncJob {

    @Autowired
    private EnvMonitorService envMonitorService;

    /**
     * 一小时执行一次
     */
    @Scheduled(cron = "${job.point.cron:0 1 * * * * }")
    public void handle() {
        Date now = new Date();
        //一小时前
        DateTime oneHourAgo = DateUtil.offsetHour(now, -1);
        List<EnvMonitorDataDto> envMonitorDataDtos = envMonitorService.selectEnvMonitorDataDtoList("6712362848558971066FFF52", DateUtil.formatDateTime(oneHourAgo), DateUtil.formatDateTime(now));
        if (CollectionUtil.isNotEmpty(envMonitorDataDtos)) {
            List<EnvMonitor> envMonitorList = envMonitorDataDtos.stream().map(envMonitorDataDto -> {
                EnvMonitor envMonitor = new EnvMonitor();
                envMonitor.setDeviceId(envMonitorDataDto.getDeviceid());
                envMonitor.setItem(envMonitorDataDto.getItem());
                envMonitor.setDTime(envMonitorDataDto.getDtime());
                envMonitor.setData(envMonitorDataDto.getData());
                envMonitor.setRssi(envMonitorDataDto.getRssi());
                envMonitor.setBattery(envMonitorDataDto.getBattery());
                EnvMonitorEnum.ItemEnum envMonitorItemEnum = EnvMonitorEnum.ItemEnum.getByName(envMonitorDataDto.getItem());
                if (Objects.nonNull(envMonitorItemEnum)) {
                    envMonitor.setItemStr(envMonitorItemEnum.getContent());
                    envMonitor.setUnit(envMonitorItemEnum.getUnit());
                } else {
                    if ("FX".equals(envMonitorDataDto.getItem())) {
                        //风向
                        EnvMonitorEnum.FxEnum fxEnum = EnvMonitorEnum.FxEnum.getByCode(envMonitorDataDto.getData());
                        if (Objects.nonNull(fxEnum)) {
                            envMonitor.setData(fxEnum.getContent());
                            envMonitor.setItemStr("风向");
                        }
                    } else if ("YX".equals(envMonitorDataDto.getItem())) {
                        if (StringUtils.isNotBlank(envMonitorDataDto.getData())) {
                            envMonitor.setData("0".equals(envMonitorDataDto.getData()) ? "正常" : "报警");
                            envMonitor.setItemStr("雨雪");
                        }
                    }
                }
                envMonitor.setCreateTime(now);
                envMonitor.setUpdateTime(now);
                return envMonitor;
            }).collect(Collectors.toList());
            envMonitorService.insertBatch(envMonitorList);
        }
    }
}
